package com.shop.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyTest {
    public static void main(String[] args) {

        JDKProxyTest jdkProxyTest  = new JDKProxyTest();

        MyInterHandler interHandler= new MyInterHandler(jdkProxyTest);
        TargetInterface target = (TargetInterface) Proxy.newProxyInstance(MyTest.class.getClassLoader(),new Class[]{TargetInterface.class},interHandler);
        target.sayHello();
    }


    static class MyInterHandler implements InvocationHandler {

        private Object subObject ;

        public MyInterHandler(Object o){
            this.subObject = o ;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("before call method "+method.getName());
            System.out.println("do something before ...");
            Object invoke = method.invoke(subObject, args);
            System.out.println("after call method ...");
            return invoke;
        }
    }
}
